//全屏
var btn = document.getElementById("btn");
 
btn.onclick = function() {
    var width =  window.screen.width;
    var height =   window.screen.height;
    var allbody = document.getElementById("allbody");
    requestFullScreen(allbody);
}
 
function requestFullScreen(element) {
    if (element.requestFullscreen) {
        element.requestFullscreen();
    }
//FireFox
    else if (element.mozRequestFullScreen) {
        element.mozRequestFullScreen();
    }
//Chrome等
    else if (element.webkitRequestFullScreen) {
        element.webkitRequestFullScreen();
    }
//IE11
    else if (element.msRequestFullscreen) {
        element.msRequestFullscreen();
    }
};
//增加
	var addscan=document.getElementById("addscan");
	var nameid=document.getElementsByClassName("nameid");
	var td=document.getElementsByClassName("td");
	var ta=document.getElementsByClassName("ta");
	var newadd=document.getElementsByClassName("newadd")[0];
	var clearBtn=addscan.getElementsByTagName("input");
	
	newadd.onclick=function(){
		console.log(nameid[0])
		$.ajax({
			type:"post",
			url:"http://118.195.129.130:3000/food/add",
			async:true,
			data:{
				name:nameid[0].value,
				price:nameid[1].value,
				desc:nameid[2].value,
				typename:nameid[3].value,
				typeid:nameid[4].value,
			},
			success:function(data){
				alert(data.msg);
				search();
				for(let i=0;i<clearBtn.length;i++){
					clearBtn[i].value="";
				}
			},
			error:function(err){
				console.log(err);
			}
		})
	}
	search();
	var dish = document.getElementsByClassName('dish');
	function search(i){
		$.ajax({
			type:"post",
			url:"http://118.195.129.130:3000/food/getInfoByPage",
			async:true,
			data:{
				page:i,
				per_page:5
			},
			success:function(data){
				for(let i=0;i<dish.length;i++){
					dish[i].children[0].innerHTML = data.data[i].name;
					dish[i].children[1].innerHTML = data.data[i].price;
					dish[i].children[2].innerHTML = data.data[i].desc;
					dish[i].children[3].innerHTML = data.data[i].typename;
					dish[i].children[4].innerHTML = data.data[i].typeid;
					dish[i].children[5].innerHTML = data.data[i]._id;
				}
				console.log(data.data[0])
			},
			error:function(err){
				console.log(err);
			}
		})
	}
var pages=document.getElementsByClassName("page");
for(let i=0;i<pages.length;i++){
	pages[i].onclick=function(){
		console.log(i+1);
		search(i+1);
	}
}
//搜索
	var scan=document.getElementsByClassName("scan")[0];
	var searchByName=document.getElementsByClassName("searchByName")[0];
	var dish=document.getElementsByClassName("dish");
	var getnew=document.getElementsByClassName("getnew")[0];
	var tbody=document.getElementById("tbody");
	var firsttable=document.getElementById("table");
	var nameid=scan.getElementsByClassName("nameid"[0]);
	scan.onclick=function(){
		$.ajax({
			type:"post",
			url:"http://118.195.129.130:3000/food/getInfoByKw",
			async:true,
			data:{
				kw:searchByName.value
			},
			success:function(data){
				//清空表格
//				$('#tbody').empty();
//				$('.dish').empty();
//				alert(data.data.length);
			if(data.data.length==0){
				alert("你搜索的不在此范围内，请重新输入！");
			}else{
				var tbody=document.getElementById("tbody");
				tbody.innerHTML='';
				//创建表格
				for(let i=0;i<data.data.length;i++){
					var tr=document.createElement("tr");
					tbody.appendChild(tr);
					for(let j=0;j<7;j++){
						var td=document.createElement("td");
						if(j == 0){
							td.innerHTML = data.data[i].name;
						}
						if(j == 1){
							td.innerHTML = data.data[i].price;
						}
						if(j == 2){
							td.innerHTML = data.data[i].desc;
						}
						if(j == 3){
							td.innerHTML = data.data[i].typeid;
						}
						if(j == 4){
							td.innerHTML = data.data[i].typename;
						}
						if(j == 5){
							td.innerHTML = data.data[i]._id;
						}
						else if(j==6){
							td.innerHTML = "<button class='del'>删除<button class='change'>修改";
						}
						tr.appendChild(td);
							
					}
					
				}		
			}	
				console.log(data);
//				search();
			},
			error:function(err){
				console.log(err);
			}
		})
	}
	getnew.onclick=function(){
		nameid.value="";
		location.reload();
	}
//删除
var dels=document.getElementsByClassName("del");
var delHide=document.getElementsByClassName("delHide")[0];
var getId=document.getElementsByClassName("getId")[0];
var delyesbtn=document.getElementsByClassName("delyesbtn")[0];
var delnobtn=document.getElementsByClassName("delnobtn")[0];
for(let i=0;i<dels.length;i++){
	dels[i].onclick=function(){
		var _id=this.parentNode.parentNode.children[5].innerHTML;
		delHide.style.display="block";
		getId.innerHTML=_id;
		delyesbtn.onclick=function(){
			console.log( _id)
			$.ajax({
				type:"post",
				url:"http://118.195.129.130:3000/food/del",
				async:true,
				data:{
					_id:_id
				},
				success:function(data){
					delHide.style.display="none";
					console.log(data);
					search();
					
				},
				error:function(err){
					console.log("错误");
				}
			});
		}
		delnobtn.onclick=function(){
			delHide.style.display="none";
		}
	}
}
//修改
var changes=document.getElementsByClassName("change");
var name=document.getElementsByClassName("name");
var changetext=document.getElementsByClassName("changetext");
var changeHide=document.getElementsByClassName("changeHide")[0];
var changeYesBtn=document.getElementsByClassName("changeYesBtn")[0];
var changeNoBtn=document.getElementsByClassName("changeNoBtn")[0];
for(let i=0;i<changes.length;i++){
	changes[i].onclick=function(){
		changeHide.style.display="block";
		var names=this.parentNode.parentNode.childNodes[1].innerHTML;
		var price=this.parentNode.parentNode.childNodes[3].innerHTML;
		var desc=this.parentNode.parentNode.childNodes[5].innerHTML;
		var typename=this.parentNode.parentNode.childNodes[7].innerHTML;
		var typeid=this.parentNode.parentNode.childNodes[9].innerHTML;
		var _id=this.parentNode.parentNode.childNodes[11].innerHTML;
//		console.log(this.parentNode.parentNode.childNodes);
		changetext[0].value=names;
		changetext[1].value=price;
		changetext[2].value= desc;
		changetext[3].value=typename;
		changetext[4].value=typeid;
		changetext[5].value=_id;
		changeYesBtn.onclick=function(){
			$.ajax({
				url:"http://118.195.129.130:3000/food/update",
				type:"post",
				async:true,
				data:{
					name:changetext[0].value,
					price:changetext[1].value,
					desc:changetext[2].value,
					typename:changetext[3].value,
					typeid:changetext[4].value,
					_id:changetext[5].value
				},
				success:function(data){
					search(1);
					changeHide.style.display="none";
				},
				error:function(err){
					console.log("错误");
				}
			});
		}
		changeNoBtn.onclick=function(){
			changeHide.style.display="none";
		}

	}
}
//鼠标放在诺依的头像上隐藏的内容会出现
var a=document.getElementById("hidePerson");
function showPerson(){
	a.style.display="block";
	a.style.opacity="1";
	a.style.transition="1s";
}
function offPerson(){
	a.style.display="none";
	a.style.opacity="0";
	a.style.transition="0";	
}
//目前在哪一页会显示
var pages=document.getElementsByClassName("page");
var index=0;
var clearActive=function(){
	for(var i=0;i<pages.length;i++){
		pages[i].className="page";
	}
}
var goIndex=function(){
	clearActive();
	pages[index].className="page active";
}
for(var i=0;i<pages.length;i++){
				pages[i].addEventListener('click',function(){
					var pageIndex=this.getAttribute('data-index');
					index=pageIndex;
					
					goIndex();
				})
			}
//修改个人信息
var id=localStorage.getItem("login_id");
console.log(id)
var changeInfor=document.getElementById("changeInfor");
var altertext=document.getElementsByClassName("altertext");
var alterHide=document.getElementsByClassName("alterHide")[0];
var alterYesBtn=document.getElementsByClassName("alterYesBtn")[0];
var alterNoBtn=document.getElementsByClassName("alterNoBtn")[0];
	changeInfor.onclick=function(){
		alterHide.style.display="block";
		altertext[0].value="若依";
		altertext[1].value="hhhhhhhh";
		altertext[2].value= "13413148888";
		altertext[3].value="25";
		altertext[4].value="女";
		
		alterYesBtn.onclick=function(){
			$.ajax({
				url:"http://118.195.129.130:3000/user/mod_head",
				type:"post",
				async:true,
				data:{
					us:altertext[0].value,
					_id:altertext[1].value,
					phone:altertext[2].value,
					age:altertext[3].value,
					sex:altertext[4].value,
				},
				success:function(data){
					search(1);
					alterHide.style.display="none";
					console.log(data)
				},
				error:function(err){
					console.log("错误");
				}
			});
		}
		alterNoBtn.onclick=function(){
			alterHide.style.display="none";
		}

	}
//显示个人信息
var personInfor=document.getElementById("personInfor");
var persontext=document.getElementsByClassName("persontext");
var personHide=document.getElementsByClassName("personHide")[0];
var personYesBtn=document.getElementsByClassName("personYesBtn")[0];
var personNoBtn=document.getElementsByClassName("personNoBtn")[0];
	personInfor.onclick=function(){
		
		$.ajax({
				url:"http://118.195.129.130:3000/user/inquire",
				type:"post",
				async:true,
				data:{
					_id:id
				},
				success:function(data){
					console.log(data)
//					search(1);
					//personHide.style.display="none";
					personHide.style.display="block";
				},
				error:function(err){
					console.log("错误");
				}
		});

	}
	console.log(personYesBtn)
	personYesBtn.onclick=function(){
			console.log("123")
			
	}
	personNoBtn.onclick=function(){
			personHide.style.display="none";
	}